package com.siss.tenancy.elastic.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;

import java.time.Duration;

//@ConditionalOnProperty("spring.redis.host")
@EnableCaching
@Configuration
public class RedisDynamicConfig extends CachingConfigurerSupport { //
	 
	@Value("${spring.redis.database}")
    private int database;

	@Value("${spring.redis.hostName}")
    private String hostName;
    
    @Value("${spring.redis.port}")
    private int port;
    
    @Value("${spring.redis.password}")
    private String password;
    
    @Value("${spring.redis.timeout}")
    private int timeout;
    
    @Value("${spring.redis.pool.maxIdle}")
    private int maxIdle;
    
    @Value("${spring.redis.pool.minIdle}")
    private int minIdle;
     
    
    @Value("${spring.redis.pool.maxTotal}")
    private int maxTotal;
    
    @Value("${spring.redis.pool.maxWaitMillis}")
    private long maxWaitMillis;
    
    @Value("${spring.redis.pool.testOnBorrow}")
    private boolean testOnBorrow;
    
    
    @Bean
    public RedisTemplate<String, String> redisTemplate() {    
    	DynamicRedisTemplate<String, String> template = new DynamicRedisTemplate<String, String>(database, hostName,port,password,timeout,maxIdle, minIdle, maxTotal, maxWaitMillis,testOnBorrow);
        return template;
    }

    
    @Bean
    public CacheManager cacheManager(@SuppressWarnings("rawtypes") RedisTemplate redisTemplate, RedisConnectionFactory factory) {
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        //om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofMillis(timeout))//设置缓存过期时间
                .serializeValuesWith(
                        RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)
                        );   //设置序列化器
        RedisCacheManager build = RedisCacheManager.builder(factory)
                .cacheDefaults(config)
                .build();
        return build;
    }
    
    @Bean
    @Override
    public KeyGenerator keyGenerator() {
        return (target, method, objects) -> {
            StringBuilder sb = new StringBuilder();
            sb.append(target.getClass().getName());
            sb.append("::" + method.getName() + ":");
            for (Object obj : objects) {
                sb.append(obj.toString());
            }
            return sb.toString();
        };
    }
}  